package com.wn.controller;


import com.baomidou.mybatisplus.core.injector.methods.SelectById;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wn.entity.Dept;
import com.wn.entity.Emp;
import com.wn.mapper.EmpMapper;
import com.wn.service.EmpService;
import com.wn.service.impl.EmpServiceImpl;
import com.wn.util.Result;
import net.sf.jsqlparser.statement.select.First;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Gapper
 * @since 2023-02-09
 */
@RestController
@RequestMapping("/emp")
public class EmpController {
    @Resource
    private EmpService empService;


    @RequestMapping("add")
    public Result addEmp(Emp emp) {
        empService.save(emp);
        return Result.ok();
    }

    @RequestMapping("delete/{empId}")
    public Result deleteEmp(@PathVariable Integer empId){
        empService.removeById(empId);
        return Result.ok();
    }

    @RequestMapping("update")
    public Result updateEmp(Emp emp){
        empService.updateById(emp);
        return Result.ok();
    }

    @RequestMapping("queryById/{empId}")
    public Result queryById(@PathVariable Integer empId){
        Emp empById = empService.getById(empId);
        return Result.ok().data("empById",empById);
    }

    @PostMapping("queryAll")
    public Result queryAll(){
        Page<Emp> empPage = new Page<>(1,2);
        Page<Emp> page = empService.page(empPage);
        List<Emp> list = page.getRecords();

        return Result.ok("查询成功啦").data("emps",list).data("page",page);
    }

    @PostMapping("empSerch/{keyWord}")
    public Result empSerch(@PathVariable String keyWord){
        Page<Emp> empPage = new Page<>(1, 2);
        Page<Emp> page = empService.page(empPage);
        empService.page(page, Wrappers.<Emp>lambdaQuery().like(Emp::getName,"%"+ keyWord));
        return Result.ok("查询成功").data("pageinfo",page);
    }
}

